/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 sagyf Yang. The Four Group.
 */

package app.controllers.admin.parking;

import app.Const;
import app.models.DaoKit;
import app.models.info.Floor;
import app.models.info.ShoppingMall;
import app.models.parking.Parking;
import com.github.sog.controller.BasicController;
import com.github.sog.controller.datatables.core.DatatablesCriterias;
import com.github.sog.libs.AppFunc;
import com.github.sog.plugin.sqlinxml.SqlKit;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

/**
 * <p>
 * The Parking manager.
 * </p>
 *
 * @author sagyf yang
 * @version 1.0 2014-04-12 14:36
 * @since JDK 1.6
 */
public class MainController extends BasicController {

    public static final String FORM_ATTR = "parking";

    /**
     * The index route.
     */
    public void index() {
        List<Floor> floors = Floor.dao.list();
        setAttr("floors", floors);
        render("index.ftl");
    }


    public void list() {
        final DatatablesCriterias criterias = getCriterias();

        int floor = getParaToInt("floor", 0);

        Page<Parking> parkingPage = Parking.dao.datatable(criterias, floor);

        renderDataTables(parkingPage, criterias);
    }


    public void create() {
        Parking parking = new Parking();
        setAttr(FORM_ATTR, parking);
        setAttr("mallblock", ShoppingMall.dao.malllist());
        setAttr("floors", Floor.dao.list());
        setAttr(Const.ACTION_ATTR, Const.CREATE_ACTION);
        render("item.ftl");
    }


    public void edit() {
        final int id = getParaToInt(0, 1);
        Parking format = Parking.dao.findById(id);

        setAttr(FORM_ATTR, format);
        setAttr("mallblock", ShoppingMall.dao.malllist());
        setAttr("floors", Floor.dao.list());
        setAttr(Const.ACTION_ATTR, Const.EDIT_ACTION);
        render("item.ftl");
    }


    public void remove() {
        String remove_ids = getPara(Const.DATA_PARAM);
        List<String> ids = AppFunc.COMMA_SPLITTER.splitToList(remove_ids);
        if (DaoKit.batch_remove(SqlKit.sql("parking.remove"), ids)) {
            renderSuccess();
        } else {
            renderError("删除出现错误");
        }
    }


    public void save() {
        Parking parking = getModel(Parking.class);
        if (DaoKit.isNew(parking)) {
            parking.save();
        } else {
            parking.update();
        }
        redirect("/admin/parking");
    }
}